package com.bee.gateway.server.config;

import com.bee.gateway.common.constants.GatewayRedisConst;
import com.bee.gateway.server.listener.IgnoreUriSubscriberListener;
import com.bee.gateway.server.listener.RoutesSubscriberListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author Damin.yang
 * @date 2019/07/30 14:54
 */
@Configuration
public class RedisConfig {

    @Autowired
    private RoutesSubscriberListener routesSubscriberListener;
    @Autowired
    private IgnoreUriSubscriberListener ignoreUriSubscriberListener;

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 订阅通道
        // 路由通道
        container.addMessageListener(routesSubscriberListener, new PatternTopic(GatewayRedisConst.ROUTES_CHANNEL));
        // 忽略URI通道
        container.addMessageListener(ignoreUriSubscriberListener, new PatternTopic(GatewayRedisConst.GATEWAY_IGNORE_AUTH_CHANNEL));

        return container;
    }

    @Bean(name = "jackson2JsonRedisSerializer")
    public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }

    @Bean(name = "stringRedisSerializer")
    public StringRedisSerializer getStringRedisSerializer() {
        return new StringRedisSerializer();
    }

}
